package com.xingda.packing.dmds.rule.choose;

import com.xingda.packing.dmds.domain.ProductBox;
import com.xingda.packing.dmds.domain.Wheel;
import com.xingda.packing.dmds.enums.BoxTaskType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Order(7)
@Component("wheelTorsionDMDSChoose")
@Slf4j
public class WheelTorsionChoose implements ChooseService {

    @Override
    public boolean canChoose(ProductBox productBox, Wheel wheel) {
        boolean result = can(productBox, wheel);
        if (!result) {
            log.info("box:{}, 工字轮条码：{}，工字轮扭转大小限制->{}", productBox.getBoxNo(), wheel.getBarCode(), false);
        }
        return result;
    }

    private boolean can(ProductBox productBox, Wheel wheel) {
        if (productBox.getType() == BoxTaskType.PACKING_CLEAN.getTypeId()) {
            return true;
        }
        if (productBox.getType() == BoxTaskType.PACKING_SEMI_FINISHED_DISQUALITY.getTypeId()) {
            return true;
        }
        if (!productBox.getPackDemand().getWheelTorsion().isEnable()) {
            return true;
        }
        return productBox.getType() == BoxTaskType.PACKING_FINISHED.getTypeId()
                && wheel.getTorsionValue().compareTo(productBox.getPackDemand().getWheelTorsion().getMin()) >= 0
                && wheel.getTorsionValue().compareTo(productBox.getPackDemand().getWheelTorsion().getMax()) <= 0;
    }
}
